package com.weiyi.servlet06;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/download.do")
public class DownloadDemo extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String password = req.getParameter("password");
		if("123456".equals(password)) {
			resp.setContentType("application/pdf");
			InputStream in = getServletContext().getResourceAsStream("/WEB-INF/jdbc.pdf");
			ServletOutputStream out = resp.getOutputStream();
			writeByte(in,out);
		}
	}

	private void writeByte(InputStream in, ServletOutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int len = -1;
		while((len = in.read(buffer)) != -1) {
			out.write(buffer, 0, len);;
		}
		in.close();
		out.close();
	}
}
